﻿Imports System.Reflection
Imports System.Collections.ObjectModel

''' <summary>
''' コレクションプロパティアーカイブ
''' </summary>
''' <remarks>
''' 子アーカイブ
''' </remarks>
Public Class CollectionPropertyArchive
    Inherits PropertyArchive
    Implements IDirtyObjectConverter

    ''' <summary>
    ''' アーカイブコンテナ
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>アーカイブコレクション共通ロジック</remarks>
    Private Property Container As New ArchiveContainer(AddressOf Me.ToCollectionOrNothing)

    ''' <summary>
    ''' オブジェクトをコレクションに変換します
    ''' </summary>
    ''' <param name="instance"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ToCollectionOrNothing(instance As Object) As ICollection
        Dim val = Me.PropertyInfo.GetValue(instance, Nothing)
        If val Is Nothing Then Return Nothing

        Return DirectCast(val, ICollection)
    End Function

    ''' <summary>
    ''' アイテムエンティティアーカイブを取得します
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Archives As IEnumerable(Of ItemEntityArchive)
        Get
            Return (From el In Me.Container.Archives Select el.Value)
        End Get
    End Property

    ''' <summary>
    ''' ダーティかどうかを示します
    ''' </summary>
    ''' <param name="instance"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsDirty(instance As Object) As Boolean
        Return Me.Container.IsDirty(instance)
    End Function

    ''' <summary>
    ''' アーカイブします
    ''' </summary>
    ''' <param name="instance"></param>
    ''' <remarks></remarks>
    Public Overrides Sub Regist(instance As Object)
        Me.Container.Regist(instance)
    End Sub

    ''' <summary>
    ''' 追加ダーティオブジェクトに変換します
    ''' </summary>
    ''' <param name="instance"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ToAddDirtyObjects(instance As Object) As ICollection(Of DirtyObject) Implements IDirtyObjectConverter.ToAddDirtyObjects
        Return Me.Container.ToAddDirtyObjects(instance)
    End Function

    ''' <summary>
    ''' 削除ダーティオブジェクトに変換します
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ToDeleteDirtyObjects() As ICollection(Of DirtyObject) Implements IDirtyObjectConverter.ToDeleteDirtyObjects
        Return Me.Container.ToDeleteDirtyObjects
    End Function

    ''' <summary>
    ''' ダーティオブジェクトに変換します
    ''' </summary>
    ''' <param name="instance"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ToDirtyObjects(instance As Object) As ICollection(Of DirtyObject) Implements IDirtyObjectConverter.ToDirtyObjects
        Return Me.Container.ToDirtyObjects(instance)
    End Function

    ''' <summary>
    ''' システム値属性を持っているかどうかを示します
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides ReadOnly Property HasSystemValue As Boolean
        Get
            Return False
        End Get
    End Property

    ''' <summary>
    ''' システム値属性
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides ReadOnly Property SystemValueAttribute As SpecialValueAttribute
        Get
            Return Nothing
        End Get
    End Property
End Class